<template>
    <div>
        <div>
            <el-card class="box-card">
                <el-input v-model="queryData.clientName" placeholder="请输入客户名称" size="small"></el-input>
                <el-input v-model="queryData.empName" placeholder="请输入员工名称" size="small"></el-input>
                <el-date-picker
                        size="small"
                        v-model="queryData.joinTime"
                        type="daterange"
                        range-separator="至"
                        start-placeholder="开始日期"
                        end-placeholder="结束日期">
                </el-date-picker>&nbsp;&nbsp;
                <el-button type="primary" @click="queryDataHandle(1, 10)" size="small">查询</el-button>
                <el-button size="small" @click="resetQueryData">重置</el-button>
            </el-card>
            <div class="interspaceH"></div>
            <el-card class="box-card">
                <div style="display:flex;justify-content: space-between">
                    <div style="font-size: 14px">共{{ dataSource.length }}个待分配客户</div>
                    <div>
                        <el-button type="primary" size="small" @click="assign()">分配客户</el-button>
<!--                        <el-button @click="$emit('currentHandler', 'onlineView')" size="small">分配记录</el-button>-->
                    </div>
                </div>

                <div class="interspaceH"></div>

                <el-table :data="dataSource" @selection-change="rowChange"
                          :header-cell-style="{background:'#F3F4F7',color:'#555'}" style="width: 100%">
                    <el-table-column type="selection" width="55"></el-table-column>
                    <el-table-column prop="clientName" label="客户名称" :formatter="formatter"></el-table-column>
                    <el-table-column prop="empName" label="所属员工" :formatter="formatter"></el-table-column>
                    <el-table-column label="企业标签" width="400">
                        <template slot-scope="scope">
                            <el-tag :key="index" v-for="(tag, index) in scope.row.tagList" style="margin: 0 10px 10px 0">
                                {{ tag.clientTagsName }}
                            </el-tag>
                            <div v-if="scope.row.tagList == null || scope.row.tagList.length == 0">
                                --
                            </div>
                        </template>
                    </el-table-column>
                    <el-table-column prop="joinTime" label="添加日期" :formatter="formatter"></el-table-column>
                    <el-table-column prop="clientSource" label="渠道" :formatter="formatter"></el-table-column>
                    <el-table-column prop="clientSex" label="性别" :formatter="formatter"></el-table-column>
                    <el-table-column prop="clientRate" label="客户评分" :formatter="formatter"></el-table-column>
                    <el-table-column label="操作">
                        <template slot-scope="scope">
                            <el-button @click="detailHandle(scope.row)" type="text" size="small">详情</el-button>
                        </template>
                    </el-table-column>
                </el-table>

                <div class="interspaceH"></div>

                <el-pagination @size-change="queryDataHandle(pagePagination.currentPage,$event)"
                               @current-change="queryDataHandle($event,pagePagination.pageSize)"
                               :current-page="pagePagination.currentPage"
                               :page-sizes="pagePagination.pageSizes" :page-size="pagePagination.pageSize"
                               layout="total, sizes, prev, pager, next"
                               :total="pagePagination.total">
                </el-pagination>
            </el-card>
        </div>

        <!-- 分配客户 -->
        <el-dialog title="分配客户" :visible.sync="assignVisible" width="60%" :close-on-click-modal="false">
            <div class="tip" style="margin: 0 0 20px 0"><p>可将选中的客户转接给其他员工，进行后续服务<br>
                注意：90天内客户只能被转移一次，一个客户最多只能被转移两次</p></div>

            <ListSelect @getData="getData" :listFields="listFields" :dataSource="memberData"/>
            <span slot="footer" class="dialog-footer">
                    <el-button @click="assignVisible = false">取 消</el-button>
                <el-button type="primary" @click="assignVisible = false">确 定</el-button>
                </span>
        </el-dialog>
        <ClientDetail ref="clientDetail"/>
    </div>
</template>

<script>

import ClientDetail from '@/views/customer/client/client_detail'
import ListSelect from "@/components/list_select/list_select"

const queryData = {
    clientName: '',
    empName: '',
    joinTime: ''
}

const clientSource = [
    {
        value: '0',
        label: '手机添加'
    },
    {
        value: '1',
        label: '活动添加'
    }
]

const clientSex = [
    {
        value: '1',
        label: '男'
    },
    {
        value: '0',
        label: '女'
    }
]

export default {
    components: {
        ClientDetail,
        ListSelect
    },
    data() {
        return {
            multipleSelection: [],
            assignVisible: false,
            currentPanel: 'list',
            view: false,
            queryData: {...queryData},
            dataSource: [],
            dictionary: {
                clientSource: clientSource,
                clientSex: clientSex
            },
            fieldType: {
                clientSex: "array",
                clientSource: "array",
                joinTime: "datetime"
            },
            pagePagination: {
                currentPage: 1,
                pageSizes: [10, 15, 20, 25],
                pageSize: 10,
                total: 0
            },
            memberData: [],
            listFields: {
                deptName: "所属部门",
                empName: "员工名称",
            },
        }
    },
    mounted() {
        this.queryDataHandle()
    },
    methods: {
        queryDataHandle(page = 1, row = 10) {
            let that = this;
            let queryData = {...that.queryData}

            if (queryData.joinTime != null && queryData.joinTime != "") {
                queryData['startJoinTime'] = queryData.joinTime[0]
                queryData['endJoinTime'] = queryData.joinTime[1]
                delete queryData.joinTime
            }

            let payload = {
                ...queryData,
                clientType: "1",
                sortField: "c.join_time",
                pageNum: page,
                pageSize: row
            }
            that.$request("/api/customers/getCustomersByParams/1_0", payload, "POST").then((data) => {
                that.dataSource = data.rows
                that.pagePagination['total'] = data.total
                that.pagePagination['pageSize'] = data.row
            })
        },
        resetQueryData() {
            this.queryData = {...queryData}
        },
        rowChange(val) {
            this.multipleSelection = val;
        },
        formatter(row, column) {
            let field = column.property;
            return this.$utils.formatterForLable(field, row[field], this.fieldType, this['dictionary'][field]);
        },
        detailHandle(row) {
            this.$refs.clientDetail.open(true, row)
        },
        assign() {
            let that = this

            if (!that.multipleSelection.length > 0) {
                that.$message.error("未选择客户")
                return
            }

            that.$request("/api/emp/getAllEmp/1_0", {}, "POST").then((data) => {
                that.memberData = data.rows
                that.assignVisible = true
            })
        },
        async getData(data) {
            let clients = this.multipleSelection

            let dictionary = {}

            clients.map((item) => {
                if (dictionary[item.empId] == null) {
                    dictionary[item.empId] = {
                        handover_userid: item.empId,
                        takeover_userid: data.empId,
                        external_userid: [item.externalUserId]
                    }
                } else {
                    dictionary[item.empId]['external_userid'].push(item.externalUserId)
                }
            })

            for (let key in dictionary) {
                let requestJson = JSON.stringify(dictionary[key])
                let payload = {
                    requestJson: JSON.stringify(dictionary[key]),
                    requestBody: dictionary[key],
                    clients,
                    empName: data.empName
                }

                let res = await this.$request("/api/clientMigration/transferCustomerLine/1_0", payload, "POST")
                if (res.data.state == "error") {
                    this.$message.error(res.data.msg)
                } else {
                    this.$message.success(res.data.msg)
                }
            }
            this.queryDataHandle()
            this.assignVisible = false
        },
    }
}
</script>

<style scoped>
.el-tab-pane .el-input {
    width: 150px;
}

.el-form-item__content .el-input {
    width: 100%;
}
</style>
